package com.wbdgj.ui.near;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.MessageBoardDetailAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultMessageObjectModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.MessageActivity;
import com.wbdgj.ui.home.MessageHfActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.VerifiedActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;


import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class MessageBoardDetailActivity extends BaseActivity{
    private Context context = this;
    @BindView(R.id.name)
    TextView name;

    @BindView(R.id.time)
    TextView time;

    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.message_type)
    TextView message_type;
    @BindView(R.id.plNum)
    TextView plNum;
    private LinkedTreeMap linkedTreeMap;
    private DecimalFormat decimalFormat;

    @BindView(R.id.my_PullListView)
    PullListView myPullListView;
    @BindView(R.id.my_PullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;

    private ArrayList<LinkedTreeMap> ordersList;
    private int page = 1;
    private MessageBoardDetailAdapter listAdapter;
    private String id, branchId;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_message_board_detail;
    }

    @Override
    protected void initView() {
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        linkedTreeMap = BaseApplication.getInstance().getLinkedTreeMap();
        if ((linkedTreeMap.get("user_name")+"").equals("")){
            name.setText("匿名");
        }else{
            name.setText(linkedTreeMap.get("user_name")+"");
        }
        id = decimalFormat.format(linkedTreeMap.get("id"));
        branchId = getIntent().getStringExtra("branch_id");

        time.setText(linkedTreeMap.get("book_date")+"");
        content.setText(linkedTreeMap.get("book_content")+"");
        message_type.setText(""+linkedTreeMap.get("book_sort"));
        getData(page);
        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myPullToRefreshLayout.refreshFinish(true);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        getData(page);
                        myPullToRefreshLayout.loadMoreFinish(true);
                    }
                }, 1000);
            }
        });

        findViewById(R.id.text_top_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMbUserInfo();

            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("NAME")+"").equals("null")){
                        launch(VerifiedActivity.class);
                    }else if((linkedTreeMap.get("NAME")+"").equals("")){
                        launch(VerifiedActivity.class);
                    }else{
                        startActivityForResult(new Intent(context, MessageHfActivity.class).putExtra("id", branchId).putExtra("parent", id), 1);
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1){
            getData(page);
        }
    }

    private void getData(int page) {
        if (page == 1) {
            orders(page);
        } else {
            Addorders(page);
        }
    }

    private void orders(int page) {
        Call<ResultMessageObjectModel> call = HttpAdapter.getSerives().booklistparent("", "10", ""+page,  branchId, "", id, "");
        call.enqueue(new OnResponseListener<ResultMessageObjectModel>(context) {
            @Override
            protected void onSuccess(ResultMessageObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getCode() == 200){
                    ordersList = new ArrayList<LinkedTreeMap>();
                    ordersList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    listAdapter = new MessageBoardDetailAdapter(context, "");
                    listAdapter.setData(ordersList);
                    myPullListView.setAdapter(listAdapter);
                    if (ordersList.size() < 1) {
                        myPullListView.setPullUpEnable(false);
                    } else {
                        myPullListView.setPullUpEnable(true);
                    }
                    listAdapter.notifyDataSetChanged();
                    plNum.setText("评论("+userModelBaseResultModel.getTotal()+")");
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
                }
            }
        });
    }

    private void Addorders(int page) {
        Call<ResultMessageObjectModel> call = HttpAdapter.getSerives().booklistparent("", "10", ""+page, branchId, "", id, "");
        call.enqueue(new OnResponseListener<ResultMessageObjectModel>(context) {
            @Override
            protected void onSuccess(ResultMessageObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getCode() == 200){
                    ordersList = new ArrayList<LinkedTreeMap>();
                    ordersList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
                    listAdapter.loadMoreListView(ordersList);
                    listAdapter.notifyDataSetChanged();
                    if(ordersList.size()==0){
                        ToastUtils.toastShort("已无更多");
                    }
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
                }
            }
        });
    }
}
